﻿using PmSoft;
using PmSoft.Events;
using PmSoft.Logging;

namespace ManagementApi
{
    public class ApplicationHostedService : IHostedService
    {
        private readonly IServiceProvider serviceProvider;
        private readonly ILogger logger;

        public ApplicationHostedService(IServiceProvider serviceProvider,
            ILogger<ApplicationHostedService> logger)
        {
            this.serviceProvider = serviceProvider;
            this.logger = logger;
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("=======================Application Start=======================");
            ServiceAccessor.Setup(serviceProvider);
            //注册事件处理程序
            IEnumerable<IEventMoudle> eventMoudles = serviceProvider.GetServices<IEventMoudle>();
            foreach (var eventMoudle in eventMoudles)
            {
                eventMoudle.RegisterEventHandler();
            }

            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("=======================Application Stopped=======================");

            return Task.CompletedTask;
        }
    }
}
